package com.ishopping.service.manage.service.impl;

import com.ishopping.common.core.base.service.AbstractBaseService;
import com.ishopping.service.manage.mapper.RolePermissionMapper;
import com.ishopping.api.manage.entity.RolePermission;
import com.ishopping.api.manage.service.IRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("rolePermissionService")
public class RolePermissionServiceImpl extends AbstractBaseService<RolePermission> implements IRolePermissionService {
	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	/**
	 * 通过roleId获取用户权限
	 * @param roleId
	 * @return List<Long>
	 */
	@Override
	@Transactional(readOnly = true)
	public List<RolePermission> findRolePermissionsByRoleId(Long roleId){
		return rolePermissionMapper.queryRolePermissionsByRoleId(roleId);
	}

	/**
	 * 更新用户关联的权限
	 * @param roleId
	 * @param newPermissionList
	 * @return int
	 */
	@Override
	@Transactional
	public int save(Long roleId, List<Long> newPermissionList){
		// 批量删除
		deleteByRoleId(roleId);

		// 批量插入
		int result = batchInsert(roleId, newPermissionList);
		return result;
	}

	/**
	 * 通过roleId删除其关联的权限
	 * @param roleId
	 * @return int
	 */
	@Override
	@Transactional
	public int deleteByRoleId(Long roleId){
		return rolePermissionMapper.deleteByRoleId(roleId);
	}

	/**
	 * 批量插入用户关联的权限
	 * @param newPermissionList
	 * @param roleId
	 * @return int
	 */
	@Override
	@Transactional
	public int batchInsert(Long roleId, List<Long> newPermissionList){
		return rolePermissionMapper.batchInsert(roleId, newPermissionList);
	}
}